package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Description OrderController
 * @Author zhuzi
 * @Date 2023/10/05
 */
@RequestMapping("/admin/order")
@RequiredArgsConstructor
@RestController("adminOrderController")
@Slf4j
public class OrderController {

    private final OrderService orderService;

    /**
     * 订单查询
     *
     * @param ordersPageQueryDTO OrdersPageQueryDTO
     * @return Result
     */
    @GetMapping("/conditionSearch")
    public Result<PageResult> searchOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("Get OrdersPageQueryDTO: {}", ordersPageQueryDTO);
        return Result.success(orderService.searchOrders(ordersPageQueryDTO));
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return Result
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> getStatistics() {
        return Result.success(orderService.getStatistics());
    }

    /**
     * 获取订单详情
     *
     * @param id Long
     * @return Result
     */
    @GetMapping("/details/{id}")
    public Result<OrderVO> getOrderDetails(@PathVariable Long id) {
        log.info("Get orderId: {}", id);
        return Result.success(orderService.getOrderDetails(id));
    }

    /**
     * 接单
     *
     * @param ordersConfirmDTO OrdersConfirmDTO
     * @return Result<?>
     */
    @PutMapping("/confirm")
    public Result<?> confirmOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        ordersConfirmDTO.setStatus(Orders.CONFIRMED);
        log.info("Get OrdersConfirmDTO: {}", ordersConfirmDTO);
        orderService.confirmOrder(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒单
     *
     * @param ordersRejectionDTO OrdersRejectionDTO
     * @return Result<?>
     */
    @PutMapping("/rejection")
    public Result<?> rejectionOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("Get OrdersRejectionDTO: {}", ordersRejectionDTO);
        orderService.rejectionOrder(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 商家取消订单
     *
     * @param ordersCancelDTO OrdersCancelDTO
     * @return Result<?>
     */
    @PutMapping("/cancel")
    public Result<?> cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("Get OrdersCancelDTO: {}", ordersCancelDTO);
        orderService.adminCancelOrder(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     *
     * @param id Long
     * @return Result<?>
     */
    @PutMapping("/delivery/{id}")
    public Result<?> deliveryOrder(@PathVariable Long id) {
        log.info("Get orderId: {}", id);
        orderService.deliveryOrder(id);
        return Result.success();
    }

    /**
     * 订单完成
     *
     * @param id Long
     * @return Result<?>
     */
    @PutMapping("/complete/{id}")
    public Result<?> completeOrder(@PathVariable Long id) {
        log.info("Get orderId: {}", id);
        orderService.completeOrder(id);
        return Result.success();
    }

}
